package com.ran.java_gobang_new.advice;

import com.ran.java_gobang_new.my_exception.MatchException;
import com.ran.java_gobang_new.my_exception.UserInfoException;
import com.ran.java_gobang_new.pojo.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.HandlerMethodValidationException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ran
 * Date: 2025-09-03
 * Time: 0:05
 */
@ResponseBody
@Slf4j
@ControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler
    public Result exceptionHandlere(Exception e) {
        log.error("发生异常,e: ",e);
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler
    public Result exceptionHandlere(HandlerMethodValidationException/*参数校验异常*/ e) {
        log.error("发生异常,e: {}",e.getMessage());
        return Result.fail("参数检验失败!");
    }

    @ExceptionHandler
    public Result exceptionHandlere(MethodArgumentNotValidException/*参数校验异常*/ e) {
        String defaultMessage = e.getBindingResult().getFieldError().getDefaultMessage();
        log.error("发生输入校验异常,e: {}",e.getMessage());
        return Result.fail(defaultMessage);
    }

    @ExceptionHandler
    public Result exceptionHandlere(MatchException e) {
        log.error("发生匹配异常, e:",e);
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler
    public Result exceptionHandlere(UserInfoException e) {
        log.error("发生用户异常, e:",e);
        return Result.fail(e.getMessage());
    }
}
